package com.hx.jeeadminbase.db.jeedao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hx.jeeadminbase.db.jeedao.JeeBaseDao;
import com.hx.jeeadminbase.db.utils.CallParam;
import com.hx.jeeadminbase.db.utils.ClobString;
import com.hx.jeeadminbase.db.utils.OraProcedure;
import com.hx.jeeadminbase.db.utils.SqlHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.hx.jeeadminbase.db.utils.Record;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;

/**
 * @program: jeeadmin
 * @description:
 * @author: xxm
 * @create: 2019-11-04 15:44
 **/
@Slf4j
@Repository(value = "jeeBaseDao")
public class JeeBaseDaoImpl extends SqlSessionDaoSupport implements JeeBaseDao {

    @Resource(name = "sqlSessionTemplate")
    public void setSuperSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }

    /**
     * 获取Session
     *
     * @return
     */
    public SqlSession getSession() {
        SqlSession session = getSqlSession();
        //session.clearCache();
        return session;
    }

    public int insert(String sqlId, Object entity) throws Exception {
        try {
            log.debug("BaseDao.insert(String sqlId, Object entity)=>\n" + SqlHelper.getNamespaceSql(getSession(), sqlId, entity));
        } catch (Exception e) {
        }
        return getSession().insert(sqlId, entity);
    }

    public int update(String sqlId, Object entity) throws Exception {
        try {
            log.debug("BaseDao.update(String sqlId, Object entity)=>\n" + SqlHelper.getNamespaceSql(getSession(), sqlId, entity));
        } catch (Exception e) {
        }
        return getSession().update(sqlId, entity);
    }

    public int delete(String sqlId, Object entity) throws Exception {
        try {
            log.debug("BaseDao.delete(String sqlId, Object entity)=>\n" + SqlHelper.getNamespaceSql(getSession(), sqlId, entity));
        } catch (Exception e) {
        }
        return getSession().delete(sqlId, entity);
    }

    public <E> List<E> selectList(String sqlId) throws Exception {
        try {
            log.debug("BaseDao.selectList(String sqlId, Object entity)=>\n" + SqlHelper.getNamespaceSql(getSession(), sqlId));
        } catch (Exception e) {
        }
        return getSession().selectList(sqlId);
    }

    public <E> List<E> selectList(String sqlId, Object object) throws Exception {
        try {
            log.debug("BaseDao.selectList(String sqlId, Object entity)=>\n" + SqlHelper.getNamespaceSql(getSession(), sqlId, object));
        } catch (Exception e) {
        }
        return getSession().selectList(sqlId, object);
    }

    public <E> E selectOne(String sqlId) throws Exception {
        log.debug("BaseDao.selectOne(String sqlId, Object entity)=>\n" + SqlHelper.getNamespaceSql(getSession(), sqlId));
        return getSession().selectOne(sqlId);
    }

    public <E> E selectOne(String sqlId, Object object) throws Exception {
        try {
            log.debug("BaseDao.selectOne(String sqlId, Object entity)=>\n" + SqlHelper.getNamespaceSql(getSession(), sqlId, object));
        } catch (Exception e) {
        }
        return getSession().selectOne(sqlId, object);
    }

    /**
     * 分页查询
     *
     * @param sqlId
     * @param params
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public PageInfo<?> selectPage(String sqlId, Object params, int pageIndex, int pageSize) throws Exception {
        PageHelper.startPage(pageIndex, pageSize, true);
        try {
            log.debug("BaseDao.selectPage(String sqlId, Object params, int pageIndex, int pageSize)=>\n" + SqlHelper.getNamespaceSql(getSession(), sqlId, params));
        } catch (Exception e) {
        }
        List data = getSqlSession().selectList(sqlId, params);
        PageInfo page = new PageInfo(data);
        return page;
    }

    /**
     * 通过MyBatis sqlMapper执行sql分页查询,带分页item配置
     *
     * @param sqlId
     * @param params
     * @param pageIndex
     * @param pageSize
     * @param navigatePages
     * @return
     * @throws Exception
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public PageInfo<?> selectPage(String sqlId, Object params, int pageIndex, int pageSize, int navigatePages) throws Exception {
        PageHelper.startPage(pageIndex, pageSize, true);
        try {
            log.debug("BaseDao.selectPage(String sqlId, Object params, int pageIndex, int pageSize, int navigatePages)=>\n" + SqlHelper.getNamespaceSql(getSession(), sqlId, params));
        } catch (Exception e) {
        }
        List data = getSqlSession().selectList(sqlId, params);
        PageInfo page = new PageInfo(data, navigatePages);
        return page;
    }


    @Override
    public Map<String, Object> callOraFunction(OraProcedure function) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> param = new HashMap<String, Object>();
        StringBuffer paramSql = new StringBuffer();
        //解析IN参数
        List<CallParam> inParams = function.getInParams();
        if (inParams != null) {
            for (CallParam pm : inParams) {
                if (paramSql.length() > 0) {
                    paramSql.append(",");
                }
                param.put(pm.getParamName(), pm.getParamValue());
                paramSql.append("#{" + pm.getParamName() + ",mode=IN,jdbcType=" + pm.getParamType() + "}");
            }
        }
        //解析OUT参数
        List<CallParam> outParams = function.getOutParams();
        if (outParams != null) {
            for (CallParam pm : outParams) {
                if (paramSql.length() > 0) {
                    paramSql.append(",");
                }
                param.put(pm.getParamName(), null);
                if (pm.getParamType().equals(CallParam.CURSOR)) {
                    paramSql.append("#{" + pm.getParamName() + ",mode=OUT,jdbcType=" + pm.getParamType() + ",javaType=com.hx.base.bean.Record,typeHandler=com.hx.base.dao.CursorTypeHandler}");
                } else {
                    paramSql.append("#{" + pm.getParamName() + ",mode=OUT,jdbcType=" + pm.getParamType() + "}");
                }
            }
        }
        paramSql.insert(0, "{#{" + function.getResult().getParamName() + ",mode=OUT,jdbcType=" + function.getResult().getParamType() + "} = call " + function.getFunctionName() + "(").append(")}");
        param.put("sqlstr", paramSql);
        log.debug("调用Oracle自定义函数:" + paramSql);
        log.debug("输入参数:" + param.toString());
        getSession().selectOne("System__Common__Mapper.callProcedure", param);
        result.put("result", param.get("result"));
        if (outParams != null) {
            for (CallParam pm : outParams) {
                result.put(pm.getParamName(), param.get(pm.getParamName()));
            }
        }
        return result;
    }

    @Override
    public Map<String, Object> callOraProcedure(OraProcedure procedure) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> param = new HashMap<String, Object>();
        StringBuffer paramSql = new StringBuffer();
        //解析IN参数
        List<CallParam> inParams = procedure.getInParams();
        if (inParams != null) {
            for (CallParam pm : inParams) {
                if (paramSql.length() > 0) {
                    paramSql.append(",");
                }
                param.put(pm.getParamName(), pm.getParamValue());
                paramSql.append("#{" + pm.getParamName() + ",mode=IN,jdbcType=" + pm.getParamType() + "}");
            }
        }
        //解析OUT参数
        List<CallParam> outParams = procedure.getOutParams();
        if (outParams != null) {
            for (CallParam pm : outParams) {
                if (paramSql.length() > 0) {
                    paramSql.append(",");
                }
                param.put(pm.getParamName(), null);
                if (pm.getParamType().equals(CallParam.CURSOR)) {
                    paramSql.append("#{" + pm.getParamName() + ",mode=OUT,jdbcType=" + pm.getParamType() + ",javaType=com.hx.base.bean.Record,typeHandler=com.hx.base.dao.CursorTypeHandler}");
                } else {
                    paramSql.append("#{" + pm.getParamName() + ",mode=OUT,jdbcType=" + pm.getParamType() + "}");
                }
            }
        }
        paramSql.insert(0, "{call " + procedure.getFunctionName() + "(").append(")}");
        param.put("sqlstr", paramSql);
        log.debug("调用Oracle存储过程:" + paramSql);
        log.debug("输入参数:" + param.toString());
        getSession().selectOne("System__Common__Mapper.callProcedure", param);
        if (outParams != null) {
            for (CallParam pm : outParams) {
                result.put(pm.getParamName(), param.get(pm.getParamName()));
            }
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> findBySql(String sql) throws Exception {
        Map<String, Object> valueParams = new LinkedHashMap<String, Object>();
        valueParams.put("___mybatis_sql___", sql);
        List<Map<String, Object>> rs = new ArrayList<Map<String, Object>>();
        log.debug("BaseDao.findBySql()->" + sql);
        try {
            log.debug("BaseDao.findBySql(String sql)=>\n" + SqlHelper.getNamespaceSql(getSession(), "System__Common__Mapper.findMapRecords", valueParams));
        } catch (Exception e) {
        }
        rs = getSession().selectList("System__Common__Mapper.findMapRecords", valueParams);
        return rs;
    }

    @Override
    public List<Record> selectListBySql(String sql) throws Exception {
        Map<String, Object> valueParams = new LinkedHashMap<String, Object>();
        valueParams.put("___mybatis_sql___", sql);
        log.debug("BaseDao.selectListBySql(String sql)=>" + sql);
        try {
            log.debug("BaseDao.selectListBySql(String sql)=>" + SqlHelper.getNamespaceSql(getSession(), "System__Common__Mapper.findRecords", valueParams));
        } catch (Exception e) {
        }
        return getSession().selectList("System__Common__Mapper.findRecords", valueParams);
    }

    @Override
    public List<Record> selectListBySql(String sql, Object... params) throws Exception {
        Map<String, Object> valueParams = new LinkedHashMap<String, Object>();
        String MybatisColumn = null;
        String DBColumnName = null;
        int pIndex = 1;
        for (Object v : params) {
            DBColumnName = "__PARAM__" + pIndex;
            valueParams.put(DBColumnName, v);
            if (v != null) {
                if (v.getClass().equals(Long.class) || v.getClass().equals(Integer.class) || v.getClass().equals(Short.class) || v.getClass().equals(Byte.class)
                        || v.getClass().equals(Double.class) || v.getClass().equals(Float.class) || v.getClass().equals(java.math.BigInteger.class)
                        || v.getClass().equals(java.math.BigDecimal.class) || v.getClass().equals(long.class) || v.getClass().equals(int.class) || v.getClass().equals(short.class)
                        || v.getClass().equals(byte.class) || v.getClass().equals(double.class) || v.getClass().equals(float.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=DECIMAL}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(String.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(java.util.Date.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=TIMESTAMP}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(Boolean.class) || v.equals(boolean.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=INTEGER}";
                    v = ((Boolean) v) ? 1 : 0;
                    valueParams.put(DBColumnName, v);
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                }
            } else {
                MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
            }
            pIndex++;
        }
        valueParams.put("___mybatis_sql___", sql);
        log.debug("BaseDao.selectListBySql(String sql, Object... params)=>" + sql);
        try {
            log.debug("BaseDao.selectListBySql(String sql, Object... params)=>\n" + SqlHelper.getNamespaceSql(getSession(), "System__Common__Mapper.findRecords", valueParams));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getSession().selectList("System__Common__Mapper.findRecords", valueParams);
    }

    @Override
    public Record selectOneBySql(String sql) throws Exception {
        List<Record> rs = selectListBySql(sql);
        if (rs != null && rs.size() > 0) {
            return rs.get(0);
        }
        return null;
    }

    @Override
    public Record selectOneBySql(String sql, Object... params) throws Exception {
        List<Record> rs = selectListBySql(sql, params);
        if (rs != null && rs.size() > 0) {
            return rs.get(0);
        }
        return null;
    }

    @Override
    public boolean updateBySql(String sql) throws Exception {
        log.debug("BaseDao.updateBySql()->" + sql);
        Map<String, Object> valueParams = new LinkedHashMap<String, Object>();
        valueParams.put("___mybatis_sql___", sql);
        try {
            log.debug("BaseDao.updateBySql(String sql)=>\n" + SqlHelper.getNamespaceSql(getSession(), "System__Common__Mapper.updateRecords", valueParams));
        } catch (Exception e) {
        }

        getSqlSession().update("System__Common__Mapper.updateRecords", valueParams);
        return true;
    }

    @Override
    public boolean updateBySql(String sql, Object... params) throws Exception {
        Map<String, Object> valueParams = new LinkedHashMap<String, Object>();
        String MybatisColumn = null;
        String DBColumnName = null;
        int pIndex = 1;
        for (Object v : params) {
            DBColumnName = "__PARAM__" + pIndex;
            valueParams.put(DBColumnName, v);
            if (v != null) {
                if (v.getClass().equals(Long.class) || v.getClass().equals(Integer.class) || v.getClass().equals(Short.class) || v.getClass().equals(Byte.class)
                        || v.getClass().equals(Double.class) || v.getClass().equals(Float.class) || v.getClass().equals(java.math.BigInteger.class)
                        || v.getClass().equals(java.math.BigDecimal.class) || v.getClass().equals(long.class) || v.getClass().equals(int.class) || v.getClass().equals(short.class)
                        || v.getClass().equals(byte.class) || v.getClass().equals(double.class) || v.getClass().equals(float.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=DECIMAL}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(String.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(java.util.Date.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=TIMESTAMP}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(Boolean.class) || v.equals(boolean.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=INTEGER}";
                    v = ((Boolean) v) ? 1 : 0;
                    valueParams.put(DBColumnName, v);
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(byte[].class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=BLOB}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(ClobString.class)) {
                    ClobString content = (ClobString) v;
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=CLOB}";
                    valueParams.put(DBColumnName, content.getContent());
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                }
            } else {
                MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
            }
            pIndex++;
        }
        valueParams.put("___mybatis_sql___", sql);
        log.debug("BaseDao.updateBySql(String sql,Object... params)=>" + sql);
        try {
            try {
                log.debug("BaseDao.updateBySql(String sql,Object... params)=>\n" + SqlHelper.getNamespaceSql(getSession(), "System__Common__Mapper.updateRecords", valueParams));
            } catch (Exception e) {
            }
            getSqlSession().update("System__Common__Mapper.updateRecords", valueParams);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteBySql(String sql) throws Exception {
        log.debug("BaseDao.deleteBySql(String sql)->" + sql);
        Map<String, Object> valueParams = new LinkedHashMap<String, Object>();
        valueParams.put("___mybatis_sql___", sql);
        try {
            log.debug("BaseDao.deleteBySql(String sql)=>\n" + SqlHelper.getNamespaceSql(getSession(), "System__Common__Mapper.deleteRecords", valueParams));
            getSqlSession().update("System__Common__Mapper.deleteRecords", valueParams);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteBySql(String sql, Object... params) throws Exception {
        Map<String, Object> valueParams = new LinkedHashMap<String, Object>();
        String MybatisColumn = null;
        String DBColumnName = null;
        int pIndex = 1;
        for (Object v : params) {
            DBColumnName = "__PARAM__" + pIndex;
            valueParams.put(DBColumnName, v);
            if (v != null) {
                if (v.getClass().equals(Long.class) || v.getClass().equals(Integer.class) || v.getClass().equals(Short.class) || v.getClass().equals(Byte.class)
                        || v.getClass().equals(Double.class) || v.getClass().equals(Float.class) || v.getClass().equals(java.math.BigInteger.class)
                        || v.getClass().equals(java.math.BigDecimal.class) || v.getClass().equals(long.class) || v.getClass().equals(int.class) || v.getClass().equals(short.class)
                        || v.getClass().equals(byte.class) || v.getClass().equals(double.class) || v.getClass().equals(float.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=DECIMAL}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(String.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(java.util.Date.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=TIMESTAMP}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(Boolean.class) || v.equals(boolean.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=INTEGER}";
                    v = ((Boolean) v) ? 1 : 0;
                    valueParams.put(DBColumnName, v);
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                }
            } else {
                MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
            }
            pIndex++;
        }
        valueParams.put("___mybatis_sql___", sql);
        log.debug("BaseDao.deleteBySql(String sql,Object... params)->" + sql);
        try {
            log.debug("BaseDao.deleteBySql(String sql,Object... params)=>\n" + SqlHelper.getNamespaceSql(getSession(), "System__Common__Mapper.deleteRecords", valueParams));
        } catch (Exception e1) {
        }
        try {
            getSqlSession().update("System__Common__Mapper.deleteRecords", valueParams);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean insertBySql(String sql) throws Exception {
        log.debug("BaseDao.insertBySql(String sql)->" + sql);
        Map<String, Object> valueParams = new LinkedHashMap<String, Object>();
        valueParams.put("___mybatis_sql___", sql);
        try {
            log.debug("BaseDao.insertBySql(String sql)=>\n" + SqlHelper.getNamespaceSql(getSession(), "System__Common__Mapper.insertRecords", valueParams));
        } catch (Exception e1) {
        }
        try {
            getSqlSession().insert("System__Common__Mapper.insertRecords", valueParams);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean insertBySql(String sql, Object... params) throws Exception {
        Map<String, Object> valueParams = new LinkedHashMap<String, Object>();
        String MybatisColumn = null;
        String DBColumnName = null;
        int pIndex = 1;
        for (Object v : params) {
            DBColumnName = "__PARAM__" + pIndex;
            valueParams.put(DBColumnName, v);
            if (v != null) {
                if (v.getClass().equals(Long.class) || v.getClass().equals(Integer.class) || v.getClass().equals(Short.class) || v.getClass().equals(Byte.class)
                        || v.getClass().equals(Double.class) || v.getClass().equals(Float.class) || v.getClass().equals(java.math.BigInteger.class)
                        || v.getClass().equals(java.math.BigDecimal.class) || v.getClass().equals(long.class) || v.getClass().equals(int.class) || v.getClass().equals(short.class)
                        || v.getClass().equals(byte.class) || v.getClass().equals(double.class) || v.getClass().equals(float.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=DECIMAL}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(String.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(java.util.Date.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=TIMESTAMP}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(Boolean.class) || v.equals(boolean.class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=INTEGER}";
                    v = ((Boolean) v) ? 1 : 0;
                    valueParams.put(DBColumnName, v);
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(byte[].class)) {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=BLOB}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else if (v.getClass().equals(ClobString.class)) {
                    ClobString content = (ClobString) v;
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=CLOB}";
                    valueParams.put(DBColumnName, content.getContent());
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                } else {
                    MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                    sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
                }
            } else {
                MybatisColumn = "#{" + DBColumnName + ",jdbcType=VARCHAR}";
                sql = StringUtils.replaceOnce(sql, "?", MybatisColumn);
            }
            pIndex++;
        }
        valueParams.put("___mybatis_sql___", sql);
        log.debug("BaseDao.insertBySql(String sql,Object... params)->" + sql);
        try {
            log.debug("BaseDao.insertBySql(String sql,Object... params)=>\n" + SqlHelper.getNamespaceSql(getSession(), "System__Common__Mapper.insertRecords", valueParams));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            getSqlSession().insert("System__Common__Mapper.insertRecords", valueParams);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Long getDBSequenceValue(String seqName) throws Exception {
        Long value = null;
        String sql = "select " + seqName + ".nextval as SEQ from dual";
        try {
            List<Map<String, Object>> dbrs = findBySql(sql);
            if (dbrs != null && dbrs.size() > 0) {
                Map<String, Object> map = dbrs.get(0);
                if (map.containsKey("SEQ")) {
                    Object v = map.get("SEQ");
                    if (v != null && v.getClass().equals(java.math.BigDecimal.class)) {
                        java.math.BigDecimal dv = (java.math.BigDecimal) v;
                        value = dv.longValue();
                    }
                }
            }
        } catch (Exception e) {
            log.error("BaseDao.getDBSequenceValue(" + seqName + ")->获取序列异常!\n" + e.getMessage());
            e.printStackTrace();
            throw e;
        }
        return value;
    }

}
